package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.Subscribe;
import org.w3c.dom.Text;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.adapter.AddFriends_2_Adpter;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MarriageProposalHeadBean;
import a66weding.com.jiehuntong.beans.PersonBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.MessageEvent;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * Created by admin on 2016/7/25.
 * 添加朋友的界面
 */
public class AddFriendsActivity extends Activity implements OnNetResultListener{
    private ImageView icon;
    private EditText editText;
    private TextView back_iamge;
    private TextView title;
    public static String ADDFRIENDSACTIVITY = "AddFriendsActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addfriends);
        initView();
    }
    private void initView(){

        editText = (EditText) findViewById(R.id.add_friends_editext);
        back_iamge = (TextView) findViewById(R.id.back_iamge);
        back_iamge.setText("我的帮友");
        title = (TextView) findViewById(R.id.title_tv);
        title.setText("添加朋友");
        //editext 监听
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener()  {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //先隐藏键盘
                if(actionId == EditorInfo.IME_ACTION_SEARCH) {
                    ((InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(
                                    AddFriendsActivity.this
                                            .getCurrentFocus()
                                            .getWindowToken(),
                                    InputMethodManager.HIDE_NOT_ALWAYS);
                    String content = editText.getText().toString();
                    getNetData(content);
                }
                return false;
            }
        });
    }

    private  PersonBean personBean;
    private List<PersonBean.DataBean> list;
    private  void  getNetData(String content){
        String UserUrl = Url.BASE+"Return_Information?"+"Token=00&"+"Search="+content;
        HttpRequestBean requestBean1 = new HttpRequestBean(RequestCode.POSTEQUEST, UserUrl, this,PersonBean.class,RequestCode.RETURN_INFORMATION);
        HttpHandleUtil.executeCommonPost(this,requestBean1);
    }




    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof PersonBean){
            personBean = (PersonBean) data;
            switch (personBean.getResCode()){
                case "0000":
                    list = personBean.getData();
                    Intent intent = new Intent(this,AddFrinds_2_Actvitiy.class);
                    intent.putExtra(ADDFRIENDSACTIVITY,(Serializable) list);
                    startActivity(intent);
                    break;
                case "0003":
                    Toast.makeText(AddFriendsActivity.this,personBean.getMsg(),Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }
    public void back(View view){
        finish();
    }

    @Subscribe
    public void onEvent(MessageEvent event) {
        event.getFinish();
        finish();
    }
}
